//
//  XmlRequestBody.m
//  xmltest
//
//  Created by easystudio on 7/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "XmlRequestBody.h"
#import "XmlParser.h"



@implementation XmlRequestBody

//@synthesize requestBody;
//@synthesize totolxml;

- (id)init
{
	self = [super init];
	if(self)
	{
		//requestBody = [NSString stringWithFormat:@"<?xml version=\"1.0\"?><methodCall><methodName>column.joinColumn</methodName>	<params><param><value><struct><member><name>paramData</name><value><struct><member><name>regName</name><value/></member><member><name>joinFlag</name><value><boolean>1</boolean></value></member><member><name>columnID</name><value><string>0006EFFCC6358D5B5A32CE56FDE403AA</string></value></member></struct></value></member></struct></value></param></params></methodCall>"];
        //requestBody = [NSString stringWithFormat:@""];
        xmlParser = [[XmlParser alloc] init];
        

    }
	
	return self;
}

- (void)dealloc
{
    if (xmlParser != nil) {
        [xmlParser release];
    }
    [super dealloc];
}



-(NSMutableArray *)SendRequest:(NSString *)methodName params:(NSMutableDictionary *)requestParams
{
    [self init];
    NSMutableString *xmlbody = [[NSMutableString alloc ] initWithCapacity:0];
    if (methodName == nil) {
            
    //NSString *xmlbody = [self  DealXmlRequest:methodName params:requestParams];
    [xmlbody appendString:@"<?xml version='1.0' ?><methodCall><methodName>topic.getHotTopicList</methodName><params><param><value><struct><member><name>userID</name><value><string>CC221A4546781D2B329A278D5AEDC0F7</string></value></member><member><name>paramData</name><value><struct><member><name>START_ROW</name><value><i4>0</i4></value></member><member><name>ROWS_PER_PAGE</name><value><i4>5</i4></value></member></struct></value></member></struct></value></param></params></methodCall>"];
    }
    else{
        [xmlbody appendString:[self  DealXmlRequest:methodName params:requestParams]];
        
    }
    NSLog(@"xmlbody:%@",xmlbody);
    int flag = [xmlParser SendXml:xmlbody];

    //NSLog(@"xmlbody:%@",xmlbody);
    [xmlbody release];
    
    if (flag == 1)
        return [xmlParser GetReturnArray];
    else
        return  nil;
    
}

//
-(NSMutableArray *)SendRequestNoFilter:(NSString *)methodName params:(NSMutableDictionary *)requestParams
{
    [self init];
    NSMutableString *xmlbody = [[NSMutableString alloc ] initWithCapacity:0];
    if (methodName == nil) {
        
        //NSString *xmlbody = [self  DealXmlRequest:methodName params:requestParams];
        [xmlbody appendString:@"<?xml version='1.0' ?><methodCall><methodName>topic.getHotTopicList</methodName><params><param><value><struct><member><name>userID</name><value><string>CC221A4546781D2B329A278D5AEDC0F7</string></value></member><member><name>paramData</name><value><struct><member><name>START_ROW</name><value><i4>0</i4></value></member><member><name>ROWS_PER_PAGE</name><value><i4>5</i4></value></member></struct></value></member></struct></value></param></params></methodCall>"];
    }
    else{
        [xmlbody appendString:[self  DealXmlRequest:methodName params:requestParams]];
        
    }
    NSLog(@"xmlbody:%@",xmlbody);
    int flag = [xmlParser SendXmlNoFilter:xmlbody];
    
    //NSLog(@"xmlbody:%@",xmlbody);
    [xmlbody release];
    
    if (flag == 1)
        return [xmlParser GetReturnArray];
    else
        return  nil;
    
}


-(NSString *)SendRequestGetSubURL:(NSString *)company_code
{
    [self init];
    
    return  [[NSString alloc]initWithString: [xmlParser SendXmlGetSubURL:company_code]];
    
}


-(NSString *)DealXmlRequest:(NSString *)methodName params:(NSMutableDictionary *)requestParams
{
    bool useridflag = NO, multinode = NO;
    //NSString *totolxml;// = [[NSString alloc] initWithFormat:@""];
    NSMutableString *totolxml = [[NSMutableString alloc ] initWithCapacity:0];
    //totolxml = @"";
    NSMutableString *nodexml = [[NSMutableString alloc ] initWithCapacity:0];
    NSMutableString *methodcallxml = [[NSMutableString alloc ] initWithCapacity:0];
    NSMutableString *useridxml = [[NSMutableString alloc ] initWithCapacity:0];
    NSMutableString *keyindex = [[NSMutableString alloc ] initWithCapacity:0];
    id id_keyvalue;
    NSMutableString *keyvalue = [[NSMutableString alloc ] initWithCapacity:0];
    NSMutableString *keyvaluetype = [[NSMutableString alloc ] initWithCapacity:0];
    
    if (methodName !=nil) {
        [methodcallxml appendFormat:@"<methodName>%@</methodName>", methodName];
    }
    
    if (requestParams !=nil) {
        for (id key in requestParams) {
            //deal params
            //整数都按NSNumber处理，String都按NSString类型处理，bool在keyvalue字段填写ture或false

            [keyindex appendFormat:@"%@",key];
            id_keyvalue = [requestParams objectForKey:key];
            
            [keyvaluetype appendFormat:@"%@",[id_keyvalue class]];
            [keyvalue appendFormat:@"%@",id_keyvalue];
            //NSLog(@"%@",keyvaluetype);
            if ([keyvaluetype isEqualToString:@"NSCFNumber"]) {
                [keyvaluetype setString:@"i4"]; //or i4 or i8  
            } else {
                [keyvaluetype setString:@"string"];
                if ([keyvalue isEqualToString:@"true"]) {
                    [keyvaluetype setString:@"boolean"];
                    [keyvalue setString:@"1"];
                }
                if ([keyvalue isEqualToString:@"fault"]) {
                    [keyvaluetype setString:@"boolean"];
                    [keyvalue setString:@"0"];
                } 
            }
            
            //deal xml
            if ([keyindex isEqualToString:@"userID"]) {
                useridflag = YES;
                [useridxml appendFormat:@"<member><name>%@</name><value><%@>%@</%@></value></member>", keyindex, keyvaluetype, keyvalue, keyvaluetype];
            }
            else{
                multinode = YES;
                [nodexml appendFormat:@"<member><name>%@</name><value><%@>%@</%@></value></member>", keyindex, keyvaluetype, keyvalue, keyvaluetype];
                //totolxml = [NSString stringWithFormat:@"%@%@", totolxml, nodexml];
                [totolxml appendString:nodexml];
            }
            
            [nodexml setString:@""];
            [keyindex setString:@""];
            [keyvalue setString:@""];
            [keyvaluetype setString:@""];
            
            
        } //end for loop
    }
    
    NSString *tmp;
    
    if (useridflag == YES && multinode == NO) {
        tmp = [NSString stringWithFormat:@"<?xml version=\"1.0\"?><methodCall>%@<params><param><value><struct>%@<member><name>paramData</name><value><struct/></value></member></struct></value></param></params></methodCall>", methodcallxml, useridxml];
        //tmp = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\"?><methodCall>%@<params><param><value><struct>%@<member><name>paramData</name><value><struct/></value></member></struct></value></param></params></methodCall>", methodcallxml, useridxml];   //modified 1021
    }
    else {
        tmp = [NSString stringWithFormat:@"<?xml version=\"1.0\"?><methodCall>%@<params><param><value><struct>%@<member><name>paramData</name><value><struct>%@</struct></value></member></struct></value></param></params></methodCall>", methodcallxml,useridxml, totolxml];
        //tmp = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\"?><methodCall>%@<params><param><value><struct>%@<member><name>paramData</name><value><struct>%@</struct></value></member></struct></value></param></params></methodCall>", methodcallxml,useridxml, totolxml];      //modified 1021
    }
    
    //NSLog(@"%@", tmp);
    
    [totolxml release];
    [nodexml release];
    [methodcallxml release];
    [useridxml release];
    [keyindex release];
    [keyvalue release];
    [keyvaluetype release];
    keyvalue = nil;
    
    return tmp;
    
}




@end


